package com.frcdi.qms.system.service.impl;

import com.frcdi.qms.common.core.domain.entity.SysUser;
import com.frcdi.qms.common.enums.BusinessType;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.system.domain.DatabaseBackup;
import com.frcdi.qms.system.domain.SysUserOperLog;
import com.frcdi.qms.system.mapper.DatabaseBackupMapper;
import com.frcdi.qms.system.mapper.SysUserOperLogMapper;
import com.frcdi.qms.system.service.IDatabaseBackupService;
import com.frcdi.qms.utils.LogManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据备份Service业务层处理
 *
 * @author semarl
 * @date 2022-02-11
 */
@Service
public class DatabaseBackupServiceImpl implements IDatabaseBackupService
{
    @Autowired
    private DatabaseBackupMapper databaseBackupMapper;

    @Autowired
    private SysUserOperLogMapper operLogMapper;

    /**
     * 查询数据备份
     *
     * @param id 数据备份主键
     * @return 数据备份
     */
    @Override
    public DatabaseBackup selectDatabaseBackupById(Long id)
    {
        return databaseBackupMapper.selectDatabaseBackupById(id);
    }

    /**
     * 查询数据备份列表
     *
     * @param databaseBackup 数据备份
     * @return 数据备份
     */
    @Override
    public List<DatabaseBackup> selectDatabaseBackupList(DatabaseBackup databaseBackup)
    {
        return databaseBackupMapper.selectDatabaseBackupList(databaseBackup);
    }

    /**
     * 新增数据备份
     *
     * @param databaseBackup 数据备份
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDatabaseBackup(DatabaseBackup databaseBackup)
    {
        databaseBackup.setCreateTime(DateUtils.getNowDate());
        int row = databaseBackupMapper.insertDatabaseBackup(databaseBackup);
        //添加操作日志
        String str = StringUtils.getFieldAnnotation(databaseBackup);
//        JudgeAttributesIsEqualedUtil<DatabaseBackup> t = new JudgeAttributesIsEqualedUtil<>();
//        String str = t.ObjToString(databaseBackup);
        if (StringUtils.isNotEmpty(str)) {
            if(null!= SecurityUtils.getAuthentication()){
                // 日志记录
                SysUserOperLog operLog = LogManage.getLog("系统管理", "数据备份", BusinessType.INSERT.ordinal());
                operLog.setOperAfter(str+StringUtils.toSign("描述","手动备份"));
                operLogMapper.insertSysUserOperLog(operLog);
            }else{
                // 日志记录
                SysUserOperLog operLog = LogManage.getNoUserLog("系统管理", "数据备份", BusinessType.INSERT.ordinal());
                operLog.setOperAfter("定时任务-全量备份");
                operLogMapper.insertSysUserOperLog(operLog);
            }
        }
        return row;
    }

    /**
     * 修改数据备份
     *
     * @param databaseBackup 数据备份
     * @return 结果
     */
    @Override
    public int updateDatabaseBackup(DatabaseBackup databaseBackup)
    {
        databaseBackup.setUpdateTime(DateUtils.getNowDate());
        return databaseBackupMapper.updateDatabaseBackup(databaseBackup);
    }

    /**
     * 批量删除数据备份
     *
     * @param ids 需要删除的数据备份主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDatabaseBackupByIds(Long[] ids)
    {
        StringBuilder result = new StringBuilder();
        if(ids.length > 0){
            for(Long id:ids){
                DatabaseBackup backup = selectDatabaseBackupById(id);
                result.append(StringUtils.getFieldAnnotation(backup));
                result.append(";");
            }
        }
        int row = databaseBackupMapper.deleteDatabaseBackupByIds(ids);
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "数据备份", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(String.valueOf(result.deleteCharAt(result.length()-1)));
        operLogMapper.insertSysUserOperLog(operLog);

        return row;
    }

    /**
     * 删除数据备份信息
     *
     * @param id 数据备份主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDatabaseBackupById(Long id)
    {
        DatabaseBackup oldResult = selectDatabaseBackupById(id);
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "数据备份", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(StringUtils.getFieldAnnotation(oldResult));
        operLogMapper.insertSysUserOperLog(operLog);

        return databaseBackupMapper.deleteDatabaseBackupById(id);
    }

    public Long getRoleId(){
        Long roleId = null;
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(user.getRoles().size() > 0){
            roleId = user.getRoles().get(0).getRoleId();
        }
        return roleId;
    }
}
